<template>
  <div class="student">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="学生姓名">
        <el-input v-model="formInline.user" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.region" placeholder="状态">
          <el-option label="启用" :value="1"></el-option>
          <el-option label="禁用" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button @click="removesel">选中删除</el-button>
    <template>
      <el-table
        ref="multipleTable"
        :data="reslist"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="selChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="姓名" width="120">
          <template slot-scope="scope">
            <el-image
              style="width: 40px;"
              :src="scope.row.avatar"
              :preview-src-list="srcList"
              @click="setsrc(scope.row.avatar)"
            >
            </el-image>
            {{ scope.row.nickname }}
          </template>
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          width="150"
          show-overflow-tooltip
        >
        </el-table-column>

        <el-table-column label="创建时间" width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.created_at | filtime }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="150" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.status ? "启用" : "禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="danger" @click="remove(scope.$index)">
              删除
            </el-button>
            <el-button type="primary">详情</el-button>
            <el-button type="info" @click="edit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="pagenum"
      :page-sizes="[2, 4, 6, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="copylist.length"
    >
    </el-pagination>
    <!-- 分页 -->
    <el-dialog title="修改信息" :visible.sync="show">
      <el-form :model="form">
        <el-form-item label="学生名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生名称">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生状态">
          <el-select v-model="form.status" placeholder="请选择学生状态">
            <el-option label="启用" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="ok">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      copylist: [],
      show: false,
      form: {
        mobile: "",
        name: "",
        status: "",
      },
      formInline: {
        user: "",
        region: "",
        mobile: "",
      },

      pagesize: 2,
      pagenum: 1,

      // 定义复选框的数组
      removeids: [],
      srcList: [],
    };
  },
  mounted() {
    this.$axios.get("list.json").then((res) => {
      this.list = res.data;
      this.copylist = res.data;
      console.log(this.list);
    });
  },
  methods: {
    // 删除
    remove(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(row) {
      this.show = true;
      this.form.name = row.nickname;
      this.form.mobile = row.mobile;
      this.form.status = row.status;
      this.form.uid = row.uid;
    },
    ok() {
      this.show = false;
      this.list.forEach((item) => {
        if (this.form.uid == item.uid) {
          item.nickname = this.form.name;
          item.mobile = this.form.mobile;
          item.status = this.form.status;
          item.uid = this.form.uid;
        }
      });
    },
    onSubmit() {
      if (this.formInline.region !== "") {
        this.list = this.copylist.filter((item) => {
          return (
            item.nickname.includes(this.formInline.user) &&
            item.mobile.includes(this.formInline.mobile) &&
            item.status == this.formInline.region
          );
        });
      } else {
        this.list = this.copylist.filter((item) => {
          return (
            item.nickname.includes(this.formInline.user) &&
            item.mobile.includes(this.formInline.mobile)
          );
        });
      }
    },
    onReset() {
      this.list = this.copylist;
      this.formInline.user = "";
      this.formInline.mobile = "";
      this.formInline.region = "";
    },
    sizeChange(val) {
      this.pagesize = val;
    },
    currentChange(val) {
      this.pagenum = val;
    },
    // 复选框
    selChange(val) {
      this.removeids = [];
      val.forEach((item) => {
        this.removeids.push(item.uid);
      });
    },
    removesel() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          for (let i = 0; i < this.list.length; i++) {
            if (this.removeids.indexOf(this.list[i].uid) != -1) {
              this.list.splice(i, 1);
              i--;
            }
          }
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    setsrc(src) {
      this.srcList.push(src);
    },
  },
  filters: {
    filtime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
  computed: {
    reslist() {
      return this.list.slice(
        (this.pagenum - 1) * this.pagesize,
        this.pagesize * this.pagenum
      );
    },
  },
};
</script>

<style lang="scss"></style>
